package com.ruoyi.cms.blog.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.cms.blog.domain.CmsAdvertised;
import com.ruoyi.cms.blog.domain.CmsUserAmountLog;
import com.ruoyi.cms.blog.mapper.CmsAdvertisedMapper;
import com.ruoyi.cms.blog.mapper.CmsUserAmountLogMapper;
import com.ruoyi.cms.blog.service.ICmsUserAmountLogService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 用户消费记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-13
 */
@Service
public class CmsUserAmountLogServiceImpl implements ICmsUserAmountLogService {
    @Autowired
    private CmsUserAmountLogMapper cmsUserAmountLogMapper;
    @Autowired
    private CmsAdvertisedMapper cmsAdvertisedMapper;

    /**
     * 查询用户消费记录
     *
     * @param id 用户消费记录主键
     * @return 用户消费记录
     */
    @Override
    public CmsUserAmountLog selectCmsUserAmountLogById(Long id) {
        return cmsUserAmountLogMapper.selectCmsUserAmountLogById(id);
    }

    /**
     * 查询用户消费记录列表
     *
     * @param cmsUserAmountLog 用户消费记录
     * @return 用户消费记录
     */
    @Override
    public List<CmsUserAmountLog> selectCmsUserAmountLogList(CmsUserAmountLog cmsUserAmountLog) {
        List<CmsUserAmountLog> als = cmsUserAmountLogMapper.selectCmsUserAmountLogList(cmsUserAmountLog);
        if (!CollectionUtils.isEmpty(als)) {
            als.forEach(a -> {
                CmsAdvertised ca = cmsAdvertisedMapper.getById(a.getAdId());
                a.setTitle(ca.getTitle());
            });
        }
        return als;
    }

    /**
     * 新增用户消费记录
     *
     * @param cmsUserAmountLog 用户消费记录
     * @return 结果
     */
    @Override
    public int insertCmsUserAmountLog(CmsUserAmountLog cmsUserAmountLog) {
        cmsUserAmountLog.setCreateTime(DateUtils.getNowDate());
        return cmsUserAmountLogMapper.insertCmsUserAmountLog(cmsUserAmountLog);
    }

    /**
     * 修改用户消费记录
     *
     * @param cmsUserAmountLog 用户消费记录
     * @return 结果
     */
    @Override
    public int updateCmsUserAmountLog(CmsUserAmountLog cmsUserAmountLog) {
        cmsUserAmountLog.setUpdateTime(DateUtils.getNowDate());
        return cmsUserAmountLogMapper.updateCmsUserAmountLog(cmsUserAmountLog);
    }

    /**
     * 批量删除用户消费记录
     *
     * @param ids 需要删除的用户消费记录主键
     * @return 结果
     */
    @Override
    public int deleteCmsUserAmountLogByIds(Long[] ids) {
        return cmsUserAmountLogMapper.deleteCmsUserAmountLogByIds(ids);
    }

    /**
     * 删除用户消费记录信息
     *
     * @param id 用户消费记录主键
     * @return 结果
     */
    @Override
    public int deleteCmsUserAmountLogById(Long id) {
        return cmsUserAmountLogMapper.deleteCmsUserAmountLogById(id);
    }
}
